<template>
  <div class="app-container">
    <template v-if="showPage == true">
      <div class="search-bar">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item label="关键字">
            <el-input v-model="queryParams.keyword" placeholder="昵称/手机号" maxlength="50" clearable
              @keyup.enter="handleQuery" />
          </el-form-item>
          <!-- <el-form-item label="主经营品类">
            <el-select v-model="queryParams.categoryIds" placeholder="请选择" multiple style="width: 150px;" collapse-tags
              collapse-tags-tooltip>
              <el-option :label="item.name" :value="item.id" v-for="item in categoryList" :key="item.id" />
            </el-select>
          </el-form-item> -->
          <el-form-item label="入驻时间">
            <el-date-picker v-model="tempTime" :editable="false" type="datetimerange" range-separator="~"
              start-placeholder="开始时间" end-placeholder="截止时间" value-format="YYYY-MM-DD HH:mm:ss"
              @change="handleChange" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
            <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
          </el-form-item>
          <br></br>
          <el-form-item label="主经营品类">
            <el-checkbox-group v-model="queryParams.categoryIds" size="small">
              <el-checkbox :label="item.name" :value="item.id" v-for="item in categoryList" :key="item.id" border />
            </el-checkbox-group>
          </el-form-item>
        </el-form>

      </div>
      <el-card shadow="never" class="flex flex-col h-full" body-style="height: calc(100% - 100px);">
        <BasePageTable class="h-full" :tableColumnsData="columns" :pageSize="queryParams.pageSize" :tableData="pageData"
          :total="total" :loading="loading" @handleQuery="changePage">
          <template #operation="{ row }">
            <el-button link type="primary" @click="openReport(row)">查看</el-button>
          </template>
        </BasePageTable>
      </el-card>
    </template>
    <template v-else>
      <merchantForm @backTo="goBack" :info="formInfo" />
    </template>
  </div>
</template>

<script setup lang="ts">
import merchantForm from './components/merchant-form.vue'
import merchantApi from '@/api/mall/merchant'
import CommonAPI from '@/api/system/common'

const queryParams = ref({
  keyword: '',
  startTime: '',
  endTime: '',
  pageSize: 10,
  pageNum: 1,
  categoryIds: [],
})
const showPage = ref(true)
const pageData = ref([])
const total = ref(0)
const tempTime = ref()
const loading = ref(false)
const categoryList = ref()
const formInfo = ref()
const columns = ref([
  {
    type: "index",
    label: "序号",
    width: 60,
    align: "center",
  },
  {
    label: "商户名称",
    prop: "companyAlias",
    align: "center",
  },
  {
    label: "手机号码",
    prop: "mobile",
    align: "center",
  },
  {
    label: "法人",
    prop: "legalRepresentative",
    align: "center",
  },
  {
    label: "主经营品类",
    prop: "mainCategoryName",
    align: "center",
  },
  {
    label: "入驻时间",
    prop: "createTime",
    align: "center",
  },
  {
    label: "佣金比例",
    prop: "mallCommission",
    align: "center",
    formatter: (row, column, cellValue) => {
      if (cellValue === null || cellValue === undefined) return '-'
      return `${(cellValue * 100).toFixed(2)}%`
    }
  },
  {
    label: "推送优先级",
    prop: "sort",
    align: "center",
  },
  {
    label: "操作",
    prop: "",
    align: "center",
    customSlot: "operation",
  },
])

function handleChange(val: any) {
  if (val) {
    const [startTime, endTime] = val;
    queryParams.value.startTime = startTime || "";
    queryParams.value.endTime = endTime || "";
  } else {
    queryParams.value.startTime = "";
    queryParams.value.endTime = "";
  }
}

function goBack(val: boolean) {
  showPage.value = true
  if (val) {
    handleResetQuery()
  }
}

async function handleQuery() {
  try {
    loading.value = true
    const res: any = await merchantApi.getList(queryParams.value)
    total.value = res.total
    pageData.value = res.data
    loading.value = false
  } catch (error) {
    loading.value = false
  }

}

function handleResetQuery() {
  queryParams.value.keyword = ''
  queryParams.value.startTime = ''
  queryParams.value.endTime = ''
  tempTime.value = []
  queryParams.value.pageSize = 10
  queryParams.value.pageNum = 1
  queryParams.value.categoryIds = []
  handleQuery()
}

function changePage(val: any) {
  queryParams.value.pageNum = val.pageNum;
  queryParams.value.pageSize = val.pageSize;
  handleQuery();
}

async function openReport(row: any) {
  try {
    loading.value = true
    const res = await merchantApi.getMerchantInfo(row.userId)
    formInfo.value = res.data
    loading.value = false
    showPage.value = false
  } catch (error) {
    loading.value = false
  }
}

async function getCategoryList() {
  try {
    const res = await CommonAPI.getCategoryList({
      parentId: 2,
    })
    categoryList.value = res.data
  } catch (error) { }
}

onMounted(() => {
  getCategoryList()
  handleQuery();
});
</script>
